package com.aiju.cash.ui.multiple_shops.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.aiju.app.SimpleHttpCallback;
import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.bean.Good;
import com.aiju.bean.GoodsType;
import com.aiju.cash.R;
import com.aiju.cash.core.api.ShopManagetApi;
import com.aiju.cash.ui.multiple_shops.adapter.GoodAdapter;
import com.aiju.cash.ui.multiple_shops.adapter.GoodCategoryAdapter;
import com.aiju.cash.ui.multiple_shops.bean.RelationGoodManager;
import com.aiju.cash.util.GsonHelper;
import com.aiju.support.recyclerView.DividerItemDecoration;
import com.aiju.utils.T;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-06-15 18:58
 * @version: V1.0
 * @Description: 关联商品
 */
public class RelationGoodActivity extends BaseTitleActivity {

    //当前选中分类坐标
    private int currentPosition = 0;
    //列表是否在选择状态
    private boolean isRelation;
    //当前店铺id
    private String storeId;

    private TextView addTextView;
    private RecyclerView recyclerviewCategory;
    private RecyclerView recyclerviewTeams;

    private List<GoodsType> goodsTypes;
    private GoodCategoryAdapter categoryAdapter;
    private GoodAdapter goodAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_relation_good);
        setCenterTitle("选择关联商品");

        storeId = getIntent().getStringExtra("storeId");
        recyclerviewCategory = (RecyclerView) findViewById(R.id.recyclerview_category);
        recyclerviewCategory.setLayoutManager(new LinearLayoutManager(this));
        recyclerviewCategory.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

        recyclerviewTeams = (RecyclerView) findViewById(R.id.recyclerview_teams);
        recyclerviewTeams.setLayoutManager(new LinearLayoutManager(this));
        recyclerviewTeams.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));

        getRelationGoodList();
        addTextView = (TextView) findViewById(R.id.btn_relation_good);
        addTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (goodAdapter != null) {
                    String text = addTextView.getText().toString();
                    if (text.equals("选择更多商品")) {
                        addTextView.setText("确认关联");
                        isRelation = true;
                        goodAdapter.setDelete(isRelation);
                    } else {
                        addTextView.setText("选择更多商品");
                        isRelation = false;
                        goodAdapter.setDelete(isRelation);
                        saveOrDeleteRelationGoods();
                    }
                }
            }
        });
    }

    /**
     * 关联主店商品-添加或取消关联商品 接口
     */
    private void saveOrDeleteRelationGoods() {
        String saveGoodsIds = RelationGoodManager.getInstance().getGoodsIds()[0];
        String deleteGoodsIds = RelationGoodManager.getInstance().getGoodsIds()[1];
        if (TextUtils.isEmpty(saveGoodsIds) && TextUtils.isEmpty(deleteGoodsIds)) {
            return;
        }
        new ShopManagetApi(mDialogHelper).saveOrDeleteRelationGoods(storeId, saveGoodsIds, deleteGoodsIds, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                T.ToastShort("关联商品成功");
                getRelationGoodList();
            }

            @Override
            protected void onDataFailure(int code, String msg) {
                super.onDataFailure(code, msg);
                getRelationGoodList();
            }
        });
    }

    /**
     * 获取关联商品列表
     */
    private void getRelationGoodList() {
        new ShopManagetApi(mDialogHelper).getRelationParentGoodsList(storeId, new SimpleHttpCallback() {
            @Override
            protected void onDataSuccess(String data) {
                goodsTypes = GsonHelper.convertEntities(data, GoodsType.class);
                goodsTypes.get(currentPosition).setSeleted(true);
                /*goodsTypes.get(0).getGoodsList().get(0).setIsRelation("1");*/

                RelationGoodManager.getInstance().addGoodData(goodsTypes);
                RelationGoodManager.getInstance().addOnChangeListener(new RelationGoodManager.OnChangeListener() {
                    @Override
                    public void changeView() {
                        if (categoryAdapter != null) {
                            categoryAdapter.notifyDataSetChanged();
                        }
                    }
                });
                categoryAdapter = new GoodCategoryAdapter(RelationGoodActivity.this, RelationGoodManager.getInstance());
                categoryAdapter.setOnItemClickListener(new GoodCategoryAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        currentPosition = position;
                        for (int i = 0; i < goodsTypes.size(); i++) {
                            GoodsType goodsType = goodsTypes.get(i);
                            if (position == i) {
                                goodsType.setSeleted(true);
                            } else {
                                goodsType.setSeleted(false);
                            }
                        }
                        categoryAdapter.notifyDataSetChanged();
                        setGoodListView();
                    }
                });
                recyclerviewCategory.setAdapter(categoryAdapter);
                setGoodListView();
            }
        });
    }

    /**
     * 根据当前分类位置设置商品列表
     */
    private void setGoodListView() {
        final GoodsType goodsType = RelationGoodManager.getInstance().getGoodsTypes().get(currentPosition);
        goodAdapter = new GoodAdapter(goodsType.getGoodsList());
        goodAdapter.setDelete(isRelation);
        goodAdapter.setOnItemClickListener(new GoodAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Good good = goodsType.getGoodsList().get(position);
                if (good.getCurrentRelation().equals("0")) {
                    RelationGoodManager.getInstance().addRelation(good);
                } else {
                    RelationGoodManager.getInstance().removeRelation(good);
                }
            }
        });
        recyclerviewTeams.setAdapter(goodAdapter);
    }
}
